package com.kaiwukj.android.ufamily.mvp.ui.page.keeper;

import androidx.annotation.NonNull;

import com.kaiwukj.android.mcas.di.scope.ActivityScope;
import com.kaiwukj.android.mcas.mvp.BasePresenter;
import com.kaiwukj.android.mcas.utils.RxLifecycleUtils;
import com.kaiwukj.android.ufamily.app.MyApplication;
import com.kaiwukj.android.ufamily.mvp.http.api.ApiRespCode;
import com.kaiwukj.android.ufamily.mvp.http.callback.ApiCallBack;
import com.kaiwukj.android.ufamily.mvp.http.entity.base.ListResp;
import com.kaiwukj.android.ufamily.mvp.http.entity.event.UpdateUserInfoEvent;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.KeeperOrderResult;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.KeeperResult;
import com.kaiwukj.android.ufamily.mvp.http.exception.ApiException;
import com.kaiwukj.android.ufamily.mvp.http.utils.RxUtils;

import org.greenrobot.eventbus.EventBus;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

@ActivityScope
public class KeeperPresenter extends BasePresenter<KeeperContract.Model, KeeperContract.View> {

    public static final int REPORT = 100;
    public static final int BIND = 110;
    public static final int UNBIND = 111;
    public static final int ORDER_CREATE = 120;


    @Inject
    public KeeperPresenter(KeeperContract.Model model, KeeperContract.View view) {
        super(model, view);
    }

    public void fetchKeeperList() {
        mModel.fetchCommunityKeeperList()
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<List<KeeperResult>>(mRootView) {
                    @Override
                    public void onSuccess(@NonNull List<KeeperResult> result) {
                        mRootView.showKeeperList(result);
                    }
                });
    }

    public void fetchSysKeeperList() {
        mModel.fetchSysKeeperList()
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<List<KeeperResult>>(mRootView) {
                    @Override
                    public void onSuccess(@NonNull List<KeeperResult> result) {
                        mRootView.showKeeperList(result);
                    }
                });
    }

    public void bindKeeper(String key, boolean isPick) {
        Map<String, Object> params = new HashMap<>();
        if (isPick) {
            params.put("housekeeperId", Integer.parseInt(key));
        } else {
            params.put("bindInfo", key);
        }
        mModel.bindKeeper(params)
                .doOnSubscribe(act -> mRootView.showLoading("正在绑定..."))
                .doFinally(() -> mRootView.hideLoading())
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<Integer>(mRootView, BIND) {
                    @Override
                    public void onSuccess(@NonNull Integer result) {
                        mRootView.showMessage("绑定成功");
                        EventBus.getDefault().post(new UpdateUserInfoEvent(UpdateUserInfoEvent.REQUEST));
                    }
                });
    }

    public void getMyKeeperInfo() {
        mModel.getMyKeeperInfo()
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<KeeperResult>(mRootView) {
                    @Override
                    public void onSuccess(@NonNull KeeperResult result) {
                        mRootView.showKeeperInfo(result);
                        MyApplication.getInstance().updateKeeperInfo(result);
                    }

                    @Override
                    public void onError(Throwable e) {
                        if (e instanceof ApiException) {
                            if (((ApiException) e).getErrCode() == ApiRespCode.KEEPER_UNBIND) {
                                MyApplication.getInstance().updateKeeperInfo(null);
                                mRootView.onTaskErr(UNBIND);
                            }
                        }
                        super.onError(e);
                    }
                });
    }

    public void report(int keeperId, String content) {
        mModel.addComplain(content, keeperId)
                .doOnSubscribe(act -> mRootView.showLoading("正在提交..."))
                .doFinally(() -> mRootView.hideLoading())
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<Integer>(mRootView, REPORT) {
                    @Override
                    public void onSuccess(@NonNull Integer result) {
                        mRootView.showMessage("提交成功");
                    }
                });
    }

    public void createOrder(int keeperId, int serviceId, String appointTime, String remarks) {
        mModel.addKeeperOrder(keeperId, serviceId, appointTime, remarks)
                .doOnSubscribe(act -> mRootView.showLoading("正在下单..."))
                .doFinally(() -> mRootView.hideLoading())
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<Integer>(mRootView, ORDER_CREATE) {
                    @Override
                    public void onSuccess(@NonNull Integer result) {
                        mRootView.showMessage("下单成功");
                    }
                });
    }

    public void fetchKeeperOrderHistory(int page, int pageSize) {
        mModel.fetchKeeperOrder(page, pageSize)
                .compose(RxUtils.schedulerHelper())
                .compose(RxLifecycleUtils.bindToLifecycle(mRootView))
                .subscribe(new ApiCallBack<ListResp<KeeperOrderResult>>() {
                    @Override
                    public void onSuccess(@NonNull ListResp<KeeperOrderResult> result) {
                        mRootView.showKeeperOderList(result.getList());
                    }
                });
    }


}
